package grad.nxu.recruitsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import grad.nxu.recruitsystem.domain.Feedback;
import grad.nxu.recruitsystem.dto.ReplyFeedback;
import grad.nxu.recruitsystem.service.FeedbackService;
import grad.nxu.recruitsystem.util.EmailUtil;
import grad.nxu.recruitsystem.vo.ApiResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: recruitSystem
 * @Package_name: grad.nxu.recruitsystem.controller
 * @Date: 2023/3/10 18:54
 * @Description:
 */
@RestController
@RequestMapping("/api/v1/feedback")
public class FeedbackController {
    @Autowired
    FeedbackService feedbackService;
    @Autowired
    EmailUtil emailUtil;

    /**
     * 获取所有反馈信息接口
     *
     * @param isReplied  是否仅仅获取未回复反馈信息， true则仅仅返回未回复反馈
     * @param pageSize   每页大小
     * @param pageNumber 当前页
     * @return 返回结果
     */
    @ApiOperation("获取所有反馈信息接口")
    @GetMapping("/admin/all")
    public ApiResponse<List<Feedback>> getAll(boolean isReplied, Integer pageSize, Integer pageNumber) {
        ApiResponse<List<Feedback>> apiResponse = new ApiResponse<>();
        Page<Feedback> page = new Page<>(pageNumber, pageSize);
        Page<Feedback> feedbackPage;
        try {
            if (!isReplied) {
                QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("is_reply", false);

                feedbackPage = feedbackService.page(page, queryWrapper);
            } else {
                feedbackPage = feedbackService.page(page);
            }
            apiResponse.setCode(200).setSuccess(true).setData(feedbackPage.getRecords());
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("服务器错误");
        }
        return apiResponse;
    }

    /**
     * 删除反馈信息接口
     * @param id 反馈信息id
     * @return 删除结果
     */
    @ApiOperation("删除反馈信息接口")
    @DeleteMapping("/admin/delete")
    public ApiResponse<String> deleteById(Integer id) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        try {
            feedbackService.removeById(id);
            apiResponse.setMessage("删除成功").setCode(200).setSuccess(true);
        } catch (Exception e) {
            apiResponse.setSuccess(false).setCode(500).setMessage("删除失败");
        }
        return apiResponse;
    }

    /**
     * 回复反馈信息接口
     * @param replyFeedback 回复内容
     * @return 返回回复结果
     */
    @ApiOperation("回复反馈信息接口")
    @PostMapping("/admin/reply")
    public ApiResponse<String> replyFeedback(ReplyFeedback replyFeedback) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        try {
            emailUtil.sendEmail(replyFeedback.getRecipient(), "【回复】已收到您的反馈", replyFeedback.getContent());
            apiResponse.setMessage("发送成功").setCode(200).setSuccess(true);
        } catch (Exception e) {
            apiResponse.setSuccess(false).setCode(500).setMessage("发送失败");
        }
        return apiResponse;
    }

}
